var cookieValue = $.cookie("loginAuthorize");
var sessionidValue = $.cookie("sessionid");
if (cookieValue&&cookieValue!=='null') {
    getData(sessionidValue);
} else {
    $('#login').fadeOut();
}

function getUrlParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(window.location.href);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

function getData(sessionid) {
    $.ajax({
        url: 'api/login.php',
        type: "post",
        dataType: "JSON",
        data: {
            "sessionid": sessionid
        },
        success: function (data) {
            if (data.status == 1) {
                $.cookie("loginAuthorize", true);
                $.cookie("sessionid", sessionid);
                if(!data.data){
                    data.data= [];
                }
                new Vue({
                    el: '#app',
                    data: {
                        msg: 'hello',
                        list: data
                    },
                    methods:{
                        deleteOne:function (item) {
                            var a = confirm("确定删除吗？");
                            var self = this;
                            if(a== true){
                                $.ajax({
                                    url:"api/deleteOne.php",
                                    type:"post",
                                    dataType:"JSON",
                                    data:{
                                        "id":item.id
                                    },
                                    success: function (res) {
                                        if(res.data==1){
                                            alert("删除成功");
                                            self.list.data.$remove(item);
                                        }

                                    }
                                })
                            }
                        },
                        deleteAll: function () {
                            var a = confirm("确定删除所有数据吗？");
                            var self = this;
                            if(a== true){
                                $.ajax({
                                    url:"api/deleteAll.php",
                                    type:"post",
                                    dataType:"JSON",
                                    data:{
                                        "sessionid":$.cookie("sessionid")
                                    },
                                    success: function (res) {
                                        if(res.data){
                                            alert("删除成功");
                                            self.list.data = [];
                                        }

                                    }
                                })
                            }
                        }
                    }
                });

            } else {
                alert("登录失败，密码错误！");
            }
        },
        error: function () {
            alert("网络错误，请重新提交");
        }
    });
}

$(document).ready(function () {

    var sessionid=getUrlParameterByName('sessionid');
    if(sessionid){
        getData(sessionid);
    }
    //登陆请求
    // $('.btn-login').click(function () {
    //     getData($("#sessionid").val());
    // });
    // //登陆请求
    // $('#loginout').click(function () {
    //     $.cookie("loginAuthorize", null);
    //     $.cookie("sessionid", null);
    //     window.location.reload();
    // });
});